package com.duke.shaking.utils;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Utils class to get info about device , like device id, 
 * sim id, etc.
 * @author xiaomin
 *
 */
public class DeviceUtils {
	//private Context context;
	static private TelephonyManager telephonyManager;
	static private WindowManager windowManager;
	static private String deviceId;
	static private String simSerial;
	static private String telNumber;
	static private boolean bTelNoAvailable = true;
	static private Context context;
	
	public static void init(Context context) {
		if(context != null) {
			DeviceUtils.context = context;
			telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
			windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		}
	}
	
	public static String getDeviceId() {
		if(deviceId == null) {
			deviceId = telephonyManager.getDeviceId();
		}
		return deviceId;
	}
	
	public static String getSimSerial() {
		if(simSerial == null) {
			simSerial = telephonyManager.getSimSerialNumber();
		}
		return simSerial;
	}
	
	public static String geTelNumber() {
		if(bTelNoAvailable) {
			if(telNumber == null) {
				telNumber = telephonyManager.getLine1Number();
				if(telNumber == null) {
					bTelNoAvailable = false;
				}
			}
		}
		return telNumber;
		
	}
	
	public static int dip2px(float dpValue) {
		DisplayMetrics metrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(metrics);
		final float scale = metrics.density;
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(float pxValue) {
		DisplayMetrics metrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(metrics);
		final float scale = metrics.density;
		return (int) (pxValue / scale + 0.5f);
	}
	
	public static int getDisplayWidthPix() {
		DisplayMetrics metrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(metrics);
		return metrics.widthPixels;
	}
	
}
